<?php

class AttendanceController extends BaseController {

    public function index() {

        $iCurrentCourse = CNamHoa::CurrentCourse();

        return View::make("attendance.page", [
                'sPage_Title'       => trans('navigation.mornitor.attendance'),
                'sPage_Description' => trans('navigation.mornitor-description.attendance'),
                'iCurrentCourse'    => $iCurrentCourse,
                'aClasses'          => Classes::where('course', $iCurrentCourse)->get(),
        ]);
    }

    public function postAjaxLoadUser() {

        $sDate     = CNamHoa::ConvertDate(Input::get('date'));
        $Classes   = Classes::find(Input::get('class_id'));
        $aUser     = $Classes ? $Classes->user()->thieunhi()->get() : [];
        $aUserCore = [];

        $endDate   = $sDate ? strtotime($sDate) : strtotime('-6day');
        $startDate = strtotime('-' . (6 + (7 * (int) Input::get('counter'))) . 'day', $endDate);

        $startDate = date('Y-m-d', $startDate);
        $endDate   = date('Y-m-d', $endDate);

        $aDate  = CNamHoa::SpecificDayBetweenDates($startDate, $endDate);
        $Option = Cache::get('options');

        if ($aUser->count()) {
            foreach (UserScore::chuyencan($aDate, $aUser->lists('id'))->get() as $Info) {
                $Info->score = isset($Option[$Info->scoretype][$Info->score]) ? $Option[$Info->scoretype][$Info->score]->data->name : null;

                $aUserCore[$Info->date][$Info->user_id][$Info->scoretype] = $Info;
            }
        }

        return View::make("attendance.table", [
                'aUser'     => $aUser,
                'aUserCore' => $aUserCore,
                'aDate'     => $aDate,
        ]);
    }

    public function postAjaxLoadForm() {
        $User      = User::find(Input::get('user_id'));
        $sDate     = Input::get('date');
        $Score     = UserScore::chuyencan(Input::get('date'), Input::get('user_id'))->get();
        $aScoreLog = UserScoreLog::chuyencan(Input::get('date'), Input::get('user_id'))->get();

        $aScore = [
            'LE'  => new stdClass,
            'HOC' => new stdClass,
        ];

        foreach ($Score as $Info) {
            $aScore[$Info->scoretype] = $Info;
        }

        return View::make("attendance.modal", [
                'User'      => $User,
                'aOption'   => Cache::get('options'),
                'sDate'     => $sDate,
                'aScore'    => $aScore,
                'aScoreLog' => $aScoreLog,
        ]);
    }

    public function postAjaxUpdate() {

        $Option = Cache::get('options');

        // <editor-fold defaultstate="collapsed" desc="HOC">
        $userScore = UserScore::chuyencan(Input::get('date'), Input::get('user_id'))
            ->where('scoretype', 'HOC')
            ->first();

        $bFlag1 = false;

        if (Input::get('hoc')) {
            if ($userScore === null) {
                $userScore = new UserScore;
            }

            if ($userScore->score != Input::get('hoc')) {
                $userScore->date      = Input::get('date');
                $userScore->user_id   = Input::get('user_id');
                $userScore->type      = 'CHUYEN_CAN';
                $userScore->scoretype = 'HOC';
                $userScore->score     = Input::get('hoc');
                $userScore->data      = isset($Option['HOC'][Input::get('hoc')]) ? $Option['HOC'][Input::get('hoc')]->data->minus : 0;

                $userScore->save();
                $bFlag1 = true;
            }
        } else {
            if ($userScore) {
                $userScore->delete();
                $bFlag1 = true;
            }
        }

        if ($bFlag1) {
            UserScoreLog::create([
                'date'      => Input::get('date'),
                'user_id'   => Input::get('user_id'),
                'type'      => 'CHUYEN_CAN',
                'scoretype' => 'HOC',
                'score'     => Input::get('hoc'),
                'data'      => isset($Option['HOC'][Input::get('hoc')]) ? $Option['HOC'][Input::get('hoc')]->data->minus : 0,
                'note'      => Input::get('note'),
                'updater'   => Auth::user()->id,
            ]);
        }
        // </editor-fold>
        // <editor-fold defaultstate="collapsed" desc="LE">
        $userScore = UserScore::chuyencan(Input::get('date'), Input::get('user_id'))
            ->where('scoretype', 'LE')
            ->first();

        $bFlag2 = false;

        if (Input::get('le')) {
            if ($userScore === null) {
                $userScore = new UserScore;
            }

            if ($userScore->score != Input::get('le')) {
                $userScore->date      = Input::get('date');
                $userScore->user_id   = Input::get('user_id');
                $userScore->type      = 'CHUYEN_CAN';
                $userScore->scoretype = 'LE';
                $userScore->score     = Input::get('le');
                $userScore->data      = isset($Option['LE'][Input::get('le')]) ? $Option['LE'][Input::get('le')]->data->minus : 0;

                $userScore->save();
                $bFlag2 = true;
            }
        } else {
            if ($userScore) {
                $userScore->delete();
                $bFlag2 = true;
            }
        }

        if ($bFlag2) {
            UserScoreLog::create([
                'date'      => Input::get('date'),
                'user_id'   => Input::get('user_id'),
                'type'      => 'CHUYEN_CAN',
                'scoretype' => 'LE',
                'score'     => Input::get('le'),
                'data'      => isset($Option['LE'][Input::get('le')]) ? $Option['LE'][Input::get('le')]->data->minus : 0,
                'note'      => Input::get('note'),
                'updater'   => Auth::user()->id,
            ]);
        }
        // </editor-fold>

        if ($bFlag1 || $bFlag2) {

            // Update Total ChuyenCan
            Classes::ChuyenCan(Input::get('user_id'));
        }

        $aResponse = array(
            'status'  => 200,
            'user_id' => Input::get('user_id'),
            'date'    => Input::get('date'),
            'le'      => isset($Option['LE'][Input::get('le')]) ? $Option['LE'][Input::get('le')]->data->name : Input::get('le'),
            'hoc'     => isset($Option['HOC'][Input::get('hoc')]) ? $Option['HOC'][Input::get('hoc')]->data->name : Input::get('hoc'),
        );

        return Response::json($aResponse);
    }

}
